<template>
   
  <div>
    <el-dialog title="分类添加 " :visible.sync="oppen.off">
      {{ form }}
      <el-form :model="form">
        <el-form-item label="上级分类" :label-width="formLabelWidth">
          <el-select v-model="form.pid" placeholder="顶级分类">
            <el-option label="顶级分类" :value="0"></el-option>
            <el-option
              v-for="(item, index) in lists"
              :key="index"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="分类名称" :label-width="formLabelWidth">
          <el-input v-model="form.catename" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="分类图片" :label-width="formLabelWidth">
          <el-upload
            class="avatar-uploader"
            action="#"
            :show-file-list="false"
            :auto-upload="false"
            :on-change="picture"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>

        <el-form-item label="分类状态" :label-width="formLabelWidth">
          <el-switch
            v-model="form.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="oppen.off = false">取 消</el-button>
        <el-button type="primary" @click="submit()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: ["lists", "oppen"],
  data() {
    return {
      //图片
      imageUrl: "",
      //
      form: {
        pid: 0,
        catename: "",
        img: "",
        status: 1,
      },
      formLabelWidth: "120px",
    };
  },
  methods: {
    submit() {
      console.log(this.form);
      if (!this.form.id) {
        this.$api.goodsAdd(this.form).then((res) => {
          console.log(res);
          this.$message({
            message: "分类名称添加成功",
            type: "success",
          });
        });
        this.oppen.off = false;
      } else {
        this.$api.goodsEdit(this.form).then((res) => {
          console.log(res);
          this.$message({
            message: "分类名称编辑成功",
            type: "success",
          });
        });

      }
      this.oppen.off=false
    },
    picture(file) {
      let { name, size } = file;
      if (size > 1024 * 1024 * 2) {
        this.$message({
          message: "不能超过2M",
          type: "success",
        });
      }
      let way = ["jpg", "png", "gif", "jpeg"];
      if (!way.includes(name.split(".").pop())) {
        this.$message({
          message: "非法文件",
          type: "success",
        });
      } 
      this.imageUrl = URL.createObjectURL(file.raw);
      this.form.img = file.raw;
    },
  },
  created() {},
  mounted() {
    //回填
    this.$BUS.$on("edit", (row) => {
      console.log(row);
      delete row.children;
      this.form = JSON.parse(JSON.stringify(row));
      this.imageUrl = this.$pre + this.form.img;
      console.log(this.imageUrl);
      this.oppen.off = true;
    });
  },
  components: {},
  computed: {},
  watch: {
    oppen: {
      handler(newval, oldval) {
        if (newval.oppen.off == false) {
          this.form = {
            pid: 0,
            catename: "",
            img: "",
            status: 1,
          };
        }
      },
    },
    deep: true,
  },
};
</script>

<style lang='scss' scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
